package com.bf.hotpoint.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.bf.hotpoint.common.BanWordsUtil;
import com.bf.hotpoint.domain.DetailMessage;
import com.bf.hotpoint.domain.ListMessage;
import com.bf.hotpoint.domain.Message;
import com.bf.hotpoint.enums.KeyOfJsonResultEnum;
import com.bf.hotpoint.enums.ResultCodeEnum;
import com.bf.hotpoint.service.MessageService;
import com.bf.hotpoint.service.UserService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/message")
public class MessageController {
	static Logger log = Logger.getLogger(MessageController.class.getName());

	@Autowired
	private MessageService messageService;

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/getservertime", method = { RequestMethod.POST ,RequestMethod.GET})
	public MappingJackson2JsonView getServerTime() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("now", new Date().getTime());
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/addwithimage", method = { RequestMethod.POST ,RequestMethod.GET})
	public MappingJackson2JsonView addMessage(
			@RequestParam("uuid") String uuid,
			@RequestParam("devicetoken") String deviceToken,
			@RequestParam("content") String content,
			@RequestParam("title") String title,
			@RequestParam(value = "locationx", required = false) String locationx,
			@RequestParam(value = "locationy", required = false) String locationy,
			@RequestParam(value = "image", required = false) MultipartFile image,
			@RequestParam("liveseconds") int liveSeconds,
			@RequestParam("sex") Boolean isMale,
			@RequestParam(value = "nickname", required = false) String nickName,
			@RequestParam(value = "subject", required = false) String subject,
			@RequestParam(value = "hasuserhead", required = false) Boolean hasUserHead) {

		String nickNameUtf8 = null;
		String contentUtf8 = null;
		String titleUtf8 = null;
		String subjectUtf8 = null;
		try {
			if(nickName != null)
				nickNameUtf8 = new String(nickName.getBytes("ISO8859-1"),"UTF-8");
			if(content != null)
				contentUtf8 = new String(content.getBytes("ISO8859-1"),"UTF-8");
			if(title != null)
				titleUtf8 = new String(title.getBytes("ISO8859-1"),"UTF-8");
			if(subject != null)
			subjectUtf8 = new String(subject.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			log.error(e);
			System.out.println(e.getLocalizedMessage());
			System.out.println(e.getMessage());
		} 
		
		List<String> filePathes = new ArrayList<String>();
		if (image != null) {
			String filePath = this.uploadFileHandler(UUID.randomUUID()
					.toString(), image);
			filePath = filePath.substring(filePath.lastIndexOf(File.separator)+1);
			filePathes.add(filePath);
		}
		userService.getAvailableUser(uuid);

		ResultCodeEnum resultCode = messageService.addMessage(uuid, deviceToken,contentUtf8,
				titleUtf8, filePathes, new Point(Double.parseDouble(locationx),
						Double.parseDouble(locationy)), liveSeconds, subject,nickNameUtf8,isMale,hasUserHead);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public MappingJackson2JsonView addMessageWithOutImage(
			@RequestParam("uuid") String uuid,
			@RequestParam("devicetoken") String deviceToken,
			@RequestParam("content") String content,
			@RequestParam("title") String title,
			@RequestParam("sex") Boolean isMale,
			@RequestParam(value = "locationx", required = false) String locationx,
			@RequestParam(value = "locationy", required = false) String locationy,
			@RequestParam("liveseconds") int liveSeconds,
			@RequestParam(value = "nickname", required = false) String nickName,
			@RequestParam(value = "subject", required = false) String subject,
			@RequestParam(value = "hasuserhead", required = false) Boolean hasUserHead) {
		userService.getAvailableUser(uuid);
		Map<String, Object> result = new HashMap<String, Object>();
		
		ResultCodeEnum resultCode = null;
		List<String> banWordList = BanWordsUtil.searchBanWords(content);
		if(banWordList != null && banWordList.size() > 0)
		{
			resultCode = ResultCodeEnum.CONTENT_ILLEGAL_WORD;
		}
		else
		{
			resultCode = messageService.addMessage(
					uuid,
					deviceToken,
					content,
					title,
					null,
					new Point(Double.parseDouble(locationx), Double
							.parseDouble(locationy)), liveSeconds, subject,nickName,isMale,hasUserHead);
		}
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/report", method = { RequestMethod.POST,
			RequestMethod.GET })
	public ModelAndView report(@RequestParam("reportuuid") String reportuuid,
			@RequestParam("reportedmessageId") String reportedmessageId) {
		String reportedUuid = null;
		try {
			reportedUuid = messageService.getUuidByMessageId(reportedmessageId);
		} catch (Exception e) {
			log.error(e);
			return null;
		}

		boolean isReportedAndReportSuccess = userService.setReportedAndReport(
				reportuuid, reportedUuid);
		if (isReportedAndReportSuccess) {
			messageService.reportMessageByMessageId(reportedmessageId,
					reportedUuid);
		}
		return null;
	}

	/**
	 * Upload single file using Spring Controller
	 */
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public @ResponseBody String uploadFileHandler(
			@RequestParam("name") String name,
			@RequestParam("file") MultipartFile file) {

		if (!file.isEmpty()) {

			String fileName = file.getOriginalFilename(); // get filename on
															// client's machine
			String contentType = file.getContentType(); // get content type, you
														// can recognize which
														// kind of file is, pdf
														// or image or doc etc
			long size = file.getSize(); // get file size in bytes
			
			if(contentType == null)
			{
				return null;
			}
			
			if(MediaType.IMAGE_PNG_VALUE.equals(contentType))
			{
				name = name+".png";
			}else if(MediaType.IMAGE_JPEG_VALUE.equals(contentType)){
				name = name+".jpg";
			}else if(MediaType.IMAGE_GIF_VALUE.equals(contentType)){
				name = name+".gif";
			}

			try {
				byte[] bytes = file.getBytes();

				// Create the file on server
				File serverFile = new File(this.findPathOfMessageImage(name));
				BufferedOutputStream stream = new BufferedOutputStream(
						new FileOutputStream(serverFile));
				stream.write(bytes);
				stream.close();

				//log.info("Server File Location=" + serverFile.getAbsolutePath());
				log.info("You successfully uploaded file=" + name);
				return serverFile.getAbsolutePath();
			} catch (Exception e) {
				log.error("You failed to upload " + name + " => "
						+ e.getMessage());
			}
		} else {
			log.error("You failed to upload " + name
					+ " because the file was empty.");
			return null;
		}
		return null;
	}

	/**
	 * Upload multiple file using Spring Controller
	 */
	@RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST)
	public @ResponseBody String uploadMultipleFileHandler(
			@RequestParam("name") String[] names,
			@RequestParam("file") MultipartFile[] files) {

		if (files.length != names.length)
			return "Mandatory information missing";

		String message = "";
		for (int i = 0; i < files.length; i++) {
			MultipartFile file = files[i];
			String name = names[i];
			try {
				byte[] bytes = file.getBytes();

				// Create the file on server
				File serverFile = new File(this.findPathOfMessageImage(name));
				BufferedOutputStream stream = new BufferedOutputStream(
						new FileOutputStream(serverFile));
				stream.write(bytes);
				stream.close();

				log.info("Server File Location=" + serverFile.getAbsolutePath());

				message = message + "You successfully uploaded file=" + name
						+ "<br />";
			} catch (Exception e) {
				return "You failed to upload " + name + " => " + e.getMessage();
			}
		}
		return message;
	}

	@RequestMapping(value = "/getlistmessage", method = { RequestMethod.POST })
	public MappingJackson2JsonView getListMessages(
			@RequestParam("isnew") boolean newOrHot,
			@RequestParam("lastIndex") int lastIndex,
			@RequestParam(value = "subject", required = false) String subject,
			@RequestParam(value = "locationx", required = false) Double locationx,
			@RequestParam(value = "locationy", required = false) Double locationy,
			@RequestParam(value = "startSearchTime", required = false) Long startSearchTime) {
		System.out.println("getlistmessage");
		List<Message> messages = null;
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		Map<String, Object> result = new HashMap<String, Object>();
		ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;
		try {
			messages = messageService.getMessages(newOrHot, lastIndex,
					subject, locationx,
					locationy,startSearchTime);

			if (messages == null || messages.size() < 1) {
				resultCode = ResultCodeEnum.GET_MESSAGE_IS_NULL;
			} else {
				List<ListMessage> listMessages = new ArrayList<ListMessage>();
				for (Message message : messages) {
					ListMessage listmessage = new ListMessage();
					listmessage.setContent(message.getContent());
					listmessage.setAvailableTime(message.getAvailableTime());
					listmessage.setHasImage(message.isHasImage());
					listmessage.setNickName(message.getNickName());
					listmessage.setMessageId(message.getMessageId());
					listmessage.setSex(message.getSex());
					listmessage
							.setNumberOfDespise(message.getNumberOfDespise());
					listmessage.setNumberOfPraise(message.getNumberOfPraise());
					listmessage
							.setNumberOfReplies(message.getReplies() == null ? 0
									: message.getReplies().size());
					listmessage.setUuid(message.getUuid());
					listmessage.setHasUserHead(message.getHasUserHead());
					listMessages.add(listmessage);
				}
				result.put("listMessages", listMessages);
				result.put("startSearchTime", (new Date().getTime()));
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getLocalizedMessage());
			System.out.println(e.getMessage());
			resultCode = ResultCodeEnum.GET_MESSAGE_FAILURE;
		}
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public MappingJackson2JsonView getMessages(
			@RequestParam("neworhot") boolean newOrHot,
			@RequestParam("pagenumber") int pageNnmber,
			@RequestParam("subject") String subject,
			@RequestParam("locationx") String locationx,
			@RequestParam("locationy") String locationy,
			@RequestParam(value = "startSearchTime", required = false) Long startSearchTime) {

		List<Message> messages = null;
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		Map<String, Object> result = new HashMap<String, Object>();
		ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;
		try {
			messages = messageService.getMessages(newOrHot, pageNnmber,
					subject, Double.parseDouble(locationx),
					Double.parseDouble(locationy),startSearchTime);
			result.put("messages", messages);
		} catch (Exception e) {
			e.printStackTrace();
			resultCode = ResultCodeEnum.GET_MESSAGE_FAILURE;
		}
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/getmessagebyids", method = { RequestMethod.POST })
	public MappingJackson2JsonView getMessagesByIds(
			@RequestParam("messageids") String messageids) {

		ObjectMapper mapper = new ObjectMapper();
		List<String> parameter = null;
		List<Message> messages = null;
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		Map<String, Object> result = new HashMap<String, Object>();
		ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;

		try {
			parameter = mapper.readValue(messageids, mapper.getTypeFactory()
					.constructCollectionType(List.class, String.class));
		} catch (JsonParseException e) {
			e.printStackTrace();
			log.error(e);
			resultCode = ResultCodeEnum.PARAMETER_IS_WRONG;
		} catch (JsonMappingException e) {
			e.printStackTrace();
			log.error(e);
			resultCode = ResultCodeEnum.PARAMETER_IS_WRONG;
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e);
			resultCode = ResultCodeEnum.PARAMETER_IS_WRONG;
		}
		try {
			messages = messageService.getMessagesByIds(parameter);
			result.put("messages", messages);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			resultCode = ResultCodeEnum.GET_MESSAGE_FAILURE;
		}
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/getdetailmessagebyid", method = { RequestMethod.POST })
	public MappingJackson2JsonView getDetailMessagesById(
			@RequestParam("messageid") String messageid) {
		//ObjectMapper mapper = new ObjectMapper();
		List<String> parameter = new ArrayList<String>();
		List<Message> messages = null;
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		Map<String, Object> result = new HashMap<String, Object>();
		ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;
		
		parameter.add(messageid);
		try {
			messages = messageService.getMessagesByIds(parameter);
			if (messages == null || messages.size() < 1) {
				resultCode = ResultCodeEnum.GET_MESSAGE_IS_NULL;
			} else {
				List<DetailMessage> detailMessages = new ArrayList<DetailMessage>();
				Message message = messages.get(0);
				
					DetailMessage detailMessage = new DetailMessage();
					detailMessage
							.setImageName(message.getImages() == null ? null
									: message.getImages().get(0));
					detailMessage.setLocation(message.getLocation());
					detailMessage.setMessageId(message.getMessageId());
					detailMessage.setDeviceToken(message.getDeviceToken());
					detailMessage.setUuid(message.getUuid());
					Collections.reverse(message.getReplies());
					detailMessage.setReplies(message.getReplies());
					detailMessage.setImages(message.getImages());
					detailMessages.add(detailMessage);

				result.put("detailMessage", detailMessage);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			resultCode = ResultCodeEnum.GET_MESSAGE_FAILURE;
		}
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		view.setAttributesMap(result);
		return view;
	}
	
	@RequestMapping(value = "/getdetailmessagebyids", method = { RequestMethod.POST })
	public MappingJackson2JsonView getDetailMessagesByIds(
			@RequestParam("messageids") String messageids) {

		//ObjectMapper mapper = new ObjectMapper();
		List<String> parameter = new ArrayList<String>();
		List<Message> messages = null;
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		Map<String, Object> result = new HashMap<String, Object>();
		ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;
		
		parameter.add(messageids);
		try {
			messages = messageService.getMessagesByIds(parameter);
			if (messages == null || messages.size() < 1) {
				resultCode = ResultCodeEnum.GET_MESSAGE_IS_NULL;
			} else {
				List<DetailMessage> detailMessages = new ArrayList<DetailMessage>();
				for (Message message : messages) {
					DetailMessage detailMessage = new DetailMessage();
					detailMessage
							.setImageName(message.getImages() == null ? null
									: message.getImages().get(0));
					detailMessage.setLocation(message.getLocation());
					detailMessage.setMessageId(message.getMessageId());
					detailMessage.setDeviceToken(message.getDeviceToken());
					detailMessage.setUuid(message.getUuid());
					Collections.reverse(message.getReplies());
					detailMessage.setReplies(message.getReplies());
					detailMessages.add(detailMessage);
				}
				result.put("detailMessages", detailMessages);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			resultCode = ResultCodeEnum.GET_MESSAGE_FAILURE;
		}
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/comment", method = { RequestMethod.POST })
	public MappingJackson2JsonView publishComment(
			@RequestParam("uuid") String uuid,
			@RequestParam("devicetoken") String deviceToken,
			@RequestParam("todevicetoken") String toDeviceToken,
			@RequestParam(value = "nickname",required = false) String nickName,
			@RequestParam(value = "tonickname",required = false) String toNickName,
			@RequestParam(value = "touuid",required = false) String toUuid,
			@RequestParam(value = "sex",required = false) boolean sex,
			@RequestParam("messageid") String messageid,
			@RequestParam("content") String content) {
		userService.getAvailableUser(uuid);
		ResultCodeEnum resultCode = messageService.postReply(uuid, deviceToken,content,
				messageid,nickName,toNickName,toUuid,sex,toDeviceToken);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/praiseOrDespise", method = { RequestMethod.POST })
	public void praiseOrDespise(@RequestParam("uuid") String uuid,
			@RequestParam("messageid") String messageid,
			@RequestParam("ispraiseordespise") boolean isPraiseOrDespise) {
		userService.getAvailableUser(uuid);
		messageService.praiseOrDespise(isPraiseOrDespise, uuid, messageid);
	}
	
	@RequestMapping(value = "/getphoto", method = { RequestMethod.POST })
	public MappingJackson2JsonView getPhoto(
			@RequestParam("imagename") String imageName) {

		MappingJackson2JsonView view = new MappingJackson2JsonView();
		Map<String, Object> result = new HashMap<String, Object>();
		ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;
		
		byte[] image = null;
		try {
			File serverFile = new File(this.findPathOfMessageImage(imageName));
			String path = serverFile.getAbsolutePath();
			image = this.extractBytes(path);
		} catch (IOException e) {
			log.error(e);
			e.printStackTrace();
			result.put(KeyOfJsonResultEnum.CODE.getKey(), ResultCodeEnum.HAS_NO_IMAGE);
			view.setAttributesMap(result);
			return view;
		}

		if (image == null) {
			log.info("can't find image by name:" + imageName);
			result.put(KeyOfJsonResultEnum.CODE.getKey(), ResultCodeEnum.HAS_NO_IMAGE);
			view.setAttributesMap(result);
			return view;
		}

		result.put("image", new String (Base64.encodeBase64(image)));
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		view.setAttributesMap(result);
		return view;
	}

	@RequestMapping(value = "/getbytesofphoto", method = { RequestMethod.POST })
	public HttpEntity<byte[]> getBytesOfPhoto(
			@RequestParam("imagename") String imageName) {
		byte[] image = null;
		try {
			image = this.extractBytes(imageName);
		} catch (IOException e) {
			log.error(e);
			e.printStackTrace();
		}

		if (image == null) {
			log.info("can't find image by name:" + imageName);
			return null;
		}

		HttpHeaders headers = new HttpHeaders();

		int indexOfDot = -1;
		String type = null;
		try {
			indexOfDot = imageName.lastIndexOf(".");
			type = imageName.substring(indexOfDot+1);
		} catch (Exception e) {
			log.error("image format error by name:" + imageName);
			return null;
		}

		if (type == null) {
			log.info("can't find type of image by name:" + imageName);
			return null;
		} else if ("png".equals(type.toLowerCase())) {
			headers.setContentType(MediaType.IMAGE_PNG);
		} else if ("jpg".equals(type.toLowerCase())) {
			headers.setContentType(MediaType.IMAGE_JPEG);
		}else if ("gif".equals(type.toLowerCase())) {
			headers.setContentType(MediaType.IMAGE_GIF);
		} else {
			log.info("can't identify type of image by name:" + imageName);
			return null;
		}
		headers.setContentLength(image.length);
		return new HttpEntity<byte[]>(image, headers);
	}

	public byte[] extractBytes(String imageName) throws IOException {
		// open image
		File file = new File(imageName);
		
		byte[] bFile = new byte[(int) file.length()];
		FileInputStream fileInputStream=null;
	     try {
	            //convert file into array of bytes
		    fileInputStream = new FileInputStream(file);
		    fileInputStream.read(bFile);
		    fileInputStream.close();

        }catch(Exception e){
        	e.printStackTrace();
        }
        return bFile;
	}

	public String findPathOfMessageImage(String name)
	{
		// Creating the directory to store file
		String rootPath = System.getProperty("catalina.home");
		File dir = new File(rootPath + File.separator + "messageImages");
		if (!dir.exists())
			dir.mkdirs();
		return dir.getAbsolutePath()
				+ File.separator + name;
	}
	
	@RequestMapping(value = "/getmessagebyid", method = { RequestMethod.POST })
	public MappingJackson2JsonView getMessagesById(
			@RequestParam("messageid") String messageid) {
		//ObjectMapper mapper = new ObjectMapper();
		List<String> parameter = new ArrayList<String>();
		List<Message> messages = null;
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		Map<String, Object> result = new HashMap<String, Object>();
		ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;
		
		parameter.add(messageid);
		try {
			messages = messageService.getMessagesByIds(parameter);
			if (messages == null || messages.size() < 1) {
				resultCode = ResultCodeEnum.GET_MESSAGE_IS_NULL;
			} else {
				List<DetailMessage> detailMessages = new ArrayList<DetailMessage>();
				Message message = messages.get(0);
				
				DetailMessage detailMessage = new DetailMessage();
				detailMessage
						.setImageName(message.getImages() == null ? null
								: message.getImages().get(0));
				detailMessage.setLocation(message.getLocation());
				detailMessage.setMessageId(message.getMessageId());
				detailMessage.setDeviceToken(message.getDeviceToken());
				detailMessage.setUuid(message.getUuid());
				Collections.reverse(message.getReplies());
				detailMessage.setReplies(message.getReplies());
				detailMessage.setImages(message.getImages());
				detailMessages.add(detailMessage);
					
				result.put("detailMessage", detailMessage);
				
				ListMessage listmessage = new ListMessage();
				listmessage.setContent(message.getContent());
				listmessage.setAvailableTime(message.getAvailableTime());
				listmessage.setHasImage(message.isHasImage());
				listmessage.setNickName(message.getNickName());
				listmessage.setMessageId(message.getMessageId());
				listmessage.setSex(message.getSex());
				listmessage
						.setNumberOfDespise(message.getNumberOfDespise());
				listmessage.setNumberOfPraise(message.getNumberOfPraise());
				listmessage
						.setNumberOfReplies(message.getReplies() == null ? 0
								: message.getReplies().size());
				listmessage.setUuid(message.getUuid());
				listmessage.setHasUserHead(message.getHasUserHead());
				result.put("listMessage", listmessage);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			resultCode = ResultCodeEnum.GET_MESSAGE_FAILURE;
		}
		result.put(KeyOfJsonResultEnum.CODE.getKey(), resultCode);
		view.setAttributesMap(result);
		return view;
	}
}
